//
//  TicketOrderController.m
//  lv678
//
//  Created by apple on 15/7/14.
//  Copyright (c) 2015年 lampshade. All rights reserved.
//  订单详情

#import "TicketOrderController.h"
#import "GZTicketDeatil.h"
#import "UIImageView+WebCache.h"
#import "AFNetworking.h"
#import "GZToDetail.h"
#import "GZFilterHtml.h"
#import "TFHpple.h"
#import "TicketOrderDetailController.h"
@interface TicketOrderController ()
@property(nonatomic)CGFloat labelW;
@property(nonatomic,strong)UIView *alertView;
@property(nonatomic,strong)GZToDetail *detail;
@property(nonatomic,strong)UIWebView *webView;
@end

@implementation TicketOrderController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.labelW                            = self.view.frame.size.width;
    self.view.backgroundColor              = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:self action:nil];
    self.navigationItem.title              = @"景点详情";
    [self getData];

    UIWebView *web                         = [[UIWebView alloc]init];
    web.frame                              = CGRectMake(0, 0, self.view.frame.size.width, 0);
    self.webView                           = web;
    web.delegate                           = self;
    
}
//此处跳转到下订单页面
- (void)pushViewController:(UIViewController *)viewController
                  animated:(BOOL)animated{
    TicketOrderDetailController *view = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"order"];
    view.productName                  = _orderDetail.productName;
    view.price                        = _orderDetail.price;
    view.tid                          = _orderDetail.tid;
    [self.navigationController pushViewController:view animated:YES];

}
//此处弹出价格须知界面
- (void)adverts:(UITapGestureRecognizer *)recognizer{
    UIView *alertView          = [[UIView alloc]init];
    alertView.frame            = self.view.frame;
    alertView.backgroundColor  = [UIColor blackColor];
    alertView.alpha            = 0.8;
    self.alertView             = alertView;

    UIScrollView *scroll       = [[UIScrollView alloc]init];
    scroll.frame               = _alertView.bounds;
    scroll.contentSize         = CGSizeMake(0, self.view.frame.size.height*2);
    [_alertView addSubview:scroll];

    UILabel *te                = [[UILabel alloc]init];
    GZFilterHtml *filter       = [[GZFilterHtml alloc]init];
    NSString *filterCostDetail = [filter filterHTML:_detail.costdetail];
    NSData *date               = [_detail.costdetail dataUsingEncoding:NSUTF8StringEncoding];
    [filter parseSearchResult:date];
    te.text                    = filterCostDetail;
    te.textColor               = [UIColor whiteColor];
    te.numberOfLines           = 0;
    te.lineBreakMode           = NSLineBreakByWordWrapping;
    te.frame                   = CGRectMake(10, 40, self.labelW-10, self.view.frame.size.height);
    [scroll addSubview:te];

    NSURL *url                 = [NSURL URLWithString:_detail.costdetail];
    NSURLRequest *request      = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];


    UIButton *close            = [[UIButton alloc]init];
    close.frame                = CGRectMake(5, 66, self.labelW, scroll.frame.size.height);
    [close addTarget:self action:@selector(closeView) forControlEvents:UIControlEventTouchUpInside];
    [scroll addSubview:close];
    [self.view addSubview:alertView];
    [self.view bringSubviewToFront:alertView];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    CGRect rect      = webView.frame;
    rect.size.height = webView.scrollView.contentSize.height;
    webView.frame    = rect;
}
- (void)closeView{
    [_alertView removeFromSuperview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
//设置UIImage视图
- (UIImageView *)image{
    if (!_image) {
        _image       = [[UIImageView alloc]init];
        _image.frame = CGRectMake(0, 65, self.labelW, 120);
        [_image sd_setImageWithURL:[NSURL URLWithString:_orderDetail.image] placeholderImage:nil];
        [self.view addSubview:_image];
    }
    return _image;
}
//设置产品标题
- (UILabel *)proTitle{
    if (!_proTitle) {
        _proTitle               = [[UILabel alloc]init];
        _proTitle.frame         = CGRectMake(10, 186, self.labelW, 40);
        _proTitle.font          = [UIFont systemFontOfSize:20.0];
        _proTitle.lineBreakMode = NSLineBreakByCharWrapping;
        [_proTitle setNumberOfLines:0];
        _proTitle.text          = _detail.productName;
        [self.view addSubview:_proTitle];
    }
    return _proTitle;
}
//设置价格
- (UILabel *)priceLbl{
    NSString *price     = [[[NSNumberFormatter alloc]init]stringFromNumber:_detail.price];
    NSString *sign      = @"￥";
    sign                = [sign stringByAppendingString:price];
    price               = [sign stringByAppendingString:@" 元/人"];
    if (!_priceLbl) {
    _priceLbl           = [[UILabel alloc]init];
    _priceLbl.frame     = CGRectMake(10, 226, self.labelW/3, 30);
    _priceLbl.text      = price;
    _priceLbl.textColor = [UIColor redColor];
        [self.view addSubview:_priceLbl];
    }
    return _priceLbl;
}
//设置价格须知label
- (UILabel *)advert{
    UITapGestureRecognizer *singleRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(adverts:)];
    singleRecognizer.numberOfTapsRequired    = 1;
    CGFloat priceW                           = _priceLbl.frame.size.width;
    if (!_advert) {
    _advert                                  = [[UILabel alloc]init];
    _advert.frame                            = CGRectMake(20+priceW, 226, self.labelW/3-10, 30);
    _advert.font                             = [UIFont systemFontOfSize:14.0];
    _advert.text                             = @"价格须知";
    _advert.userInteractionEnabled           = YES;
    _advert.textColor                        = [UIColor blueColor];
    _advert.layer.cornerRadius               = 10;
        [_advert addGestureRecognizer:singleRecognizer];
        [self.view addSubview:_advert];
    }
    return _advert;
}
//设置预定按钮
- (UIButton *)orderBtn{
    CGFloat priceW  = _priceLbl.frame.size.width;
    CGFloat advertW = _advert.frame.size.width;
    if (!_orderBtn) {
        _orderBtn                    = [[UIButton alloc]init];
        _orderBtn.frame              = CGRectMake(10+priceW+advertW, 226, self.labelW/3-10, 30);
        [_orderBtn setTitle:@"报名" forState:UIControlStateNormal];
        _orderBtn.tintColor          = [UIColor blackColor];
        [_orderBtn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
        _orderBtn.backgroundColor    = [UIColor blueColor];
        _orderBtn.layer.cornerRadius = 8;
        [self.view addSubview:_orderBtn];
    }
    return _orderBtn;
}
//设置位置
-(UILabel *)address{
    if (!_address) {
        _address           = [[UILabel alloc]init];
        _address.frame     = CGRectMake(10, 270, self.labelW, 30);
        _address.text      = _detail.address;
        _address.textColor = [UIColor blueColor];
        [self.view addSubview:_address];
    }
    return _address;
}
//获取数据
- (void)getData{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"UrlList.plist" ofType:nil];
    NSDictionary *tempDic                             = [NSDictionary dictionaryWithContentsOfFile:path];
    NSString *url                                     = [tempDic objectForKey:@"travelTicket"];
    AFHTTPRequestOperationManager *manager            = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer                        = [AFJSONResponseSerializer serializer];
    manager.requestSerializer                         = [AFJSONRequestSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    NSDictionary *date = @{@"queryParameters":@{@"tid":_orderDetail.tid}, @"serviceName":@"todetail"};
        [manager POST:url parameters:date success:^(AFHTTPRequestOperation *operation, id responseObject) {
            _detail = [GZToDetail detailWithDict:[responseObject valueForKey:@"dataList"]];
            [self image];
            [self proTitle];
            [self priceLbl];
            [self advert];
            [self orderBtn];
            [self address];
            [_orderBtn addTarget:self action:@selector(pushViewController:animated:) forControlEvents:UIControlEventTouchUpInside];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"error");
        }];
    
}


@end
